﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.IO;
using System.Text;

namespace HttpShare
{
    class HttpServerHTMLBody
    {
        static string Templebody1 =
@"<!DOCTYPE html>
<html>
<head>
<meta charset = ""utf-8\"" >
<title> http服务分享</title>
</head>
<body>";
        static string Templebody2 =
@"</body>
</html> ";

        public static string GetHTML(string Header)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(Templebody1);
            stringBuilder.AppendFormat("<h1>{0}</h1>",Header);
            stringBuilder.Append(Templebody2);
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 获取目录的
        /// </summary>
        /// <param name="path">完整路径</param>
        /// <param name="dir">目录</param>
        /// <returns></returns>
        public static string GetDirHTML(string path,string url)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(Templebody1);
            DirectoryInfo dir = new DirectoryInfo(path);
            //标题
            stringBuilder.Append($"<h1>{dir.FullName}</h1>");
            stringBuilder.Append($"<a href=\"javascript:;\" onclick=\"history.go(-1);\" >返回上一级</a>");
            stringBuilder.Append("<ul>");
            //获取子目录
            var subDirs = dir.GetDirectories();
            if (subDirs!=null)
            {
                foreach (var item in subDirs)
                {
                    stringBuilder.Append($"<li><a href=\"{url+ item.Name+"/"}\">{item.Name}</a></li>");
                }
            }

            //获取子文件
            var subFiles = dir.GetFiles();
            if (subDirs != null)
            {
                foreach (var item in subFiles)
                {
                    stringBuilder.Append($"<li><a href=\"{url + item.Name}\">{item.Name}</a></li>");
                }
            }
            stringBuilder.Append("</ul>");
            stringBuilder.Append(Templebody2);
            return stringBuilder.ToString();
        }
    }
}
